#串口
import serial
import time


class SerialPort():
    def __init__(self) -> None:
        #设置串口参数
        self.port = serial.Serial()
        self.port.baudrate = 2400
        self.port.bytesize = 8
        self.port.stopbits = 1
        self.port.parity = "E"
        self.port.timeout = 0.5
        self.port.port = "COM3"
        self.port.open()
        pass


    def send(self, data):
        self.port.write(data)
        pass

    def read(self):
        return self.port.read_all()
        pass

    def close(self):
        self.port.close()
        pass
    
    def isOpen(self):
        return self.port.isOpen()
        pass

    def start(self):
        #阻塞接受
        while True:
            data = self.read()
            if data:
                print(data)
                pass
            else:
                time.sleep(0.1)
                pass
            pass
if __name__ == "__main__":
    s = SerialPort()
    s.start()
    pass